package com.lfk.three.view.Activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lfk.three.R;
import com.lfk.three.con.NetWorkQueue;
import com.lfk.three.item.NetItem;
import com.lfk.three.static_info.static_info;
import com.lfk.three.static_info.url_router;
import com.lfk.three.utils.logger.Logger;
import com.lfk.three.utils.tools.SpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText user_name;
    private EditText user_psw;
    private EditText user_phone;
    private EditText user_verify_code;

    private Button register_button;
    private Button get_verify_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        initView();
    }


    private void initView() {
        user_name = (EditText) findViewById(R.id.reg_user_name);
        user_psw = (EditText) findViewById(R.id.reg_pass_word);
        user_phone = (EditText) findViewById(R.id.reg_phone_num);
        user_verify_code = (EditText) findViewById(R.id.reg_verify_code);
        get_verify_button = (Button) findViewById(R.id.reg_get_verify_button);
        register_button = (Button) findViewById(R.id.reg_register_button);

        get_verify_button.setOnClickListener(this);
        register_button.setOnClickListener(this);
    }

    private boolean findEmpty() {
        if (user_name.getText().toString().equals("")) {
            Toast.makeText(RegisterActivity.this, "username is empty", Toast.LENGTH_SHORT).show();
            return false;
        } else if (user_psw.getText().toString().equals("")) {
            Toast.makeText(RegisterActivity.this, "psw is empty", Toast.LENGTH_SHORT).show();
            return false;
        } else if (user_phone.getText().toString().equals("")) {
            Toast.makeText(RegisterActivity.this, "phone is empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    private void getVerifyCode() {
        if (findEmpty()) {
            NetWorkQueue.getInstance().addCallBack("check_verify", new StringCallback() {
                @Override
                public void onError(Call call, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    if (response.equals("-1")) {
                        Toast.makeText(RegisterActivity.this, "error in verify", Toast.LENGTH_SHORT).show();
                    } else {
                        static_info.user_verify = response;
                        SpUtils.put("user_verify", static_info.user_verify);
                    }
                }
            });

            NetWorkQueue.getInstance().
                    sendMessage(static_info.POST_DATA_NO_PARAMS,
                            new NetItem("check_verify",
                                    url_router.GET_VERIFY_CODE + user_phone.getText().toString(), null));
        }
    }

    private void Register() {
        if (!static_info.user_verify.equals("-1") &&
                static_info.user_verify.equals(user_verify_code.getText().toString())) {

            NetWorkQueue.getInstance().addCallBack("register", new StringCallback() {
                @Override
                public void onError(Call call, Exception e) {

                }

                @Override
                public void onResponse(String response) {
                    Logger.e(response);
                    if (response.equals("401")) {
                        Toast.makeText(RegisterActivity.this, "401", Toast.LENGTH_SHORT).show();
                        user_verify_code.setText("");
                    } else {
                        static_info.user_id = response;
                        static_info.user_name = user_name.getText().toString();
                        static_info.user_phone = user_phone.getText().toString();
                        SpUtils.put("user_id", response);
                        SpUtils.put("user_phone", user_phone.getText().toString());
                    }
                }
            });

            HashMap<String, String> temp = new HashMap<>();
            temp.put("pwd", user_psw.getText().toString());
            temp.put("code", user_verify_code.getText().toString());
            temp.put("name", user_name.getText().toString());
            temp.put("phone", user_phone.getText().toString());

            NetWorkQueue.getInstance().
                    sendMessage(static_info.POST_DATA,
                            new NetItem("register", url_router.REGISTER_URL, temp));
        } else {
            Toast.makeText(RegisterActivity.this, "verify error", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.reg_get_verify_button:
                getVerifyCode();
                break;
            case R.id.reg_register_button:
                Register();
                break;
        }
    }


}

